$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            $('.images').viewer();
            this.listener();
        },
        listener:function () {
            var $this = this;
            //上传按钮点击触发
            $('.upload').click(function () {
                $this.funObj().clickUploadButton($(this));
            });
            //文件选择完成触发
            $('#fileInput').change(function() {
                $this.funObj().submitForm();
            });
            //删除图片按钮
            $('.my-btn').click(function () {
                $this.funObj().deleteImgAction($(this));
            });
            
            // $('.empty').click(function () {
            //     $this.funObj().emptyImgAction($(this));
            // })
        },
        funObj:function () {
            var $this = this;
            return {
                clickUploadButton:function (dom) {
                    $('[name=sType]').val(dom.attr('nodeType'));
                    $('#fileInput').click();
                },
                submitForm:function () {
                    var formData = new FormData($('#xh_form')[0]);
                    $.ajax({
                        url: baseUrl+"hrm/staff_info/saveAccessory",
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
                        complete: function(XMLHttpRequest){
                            xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                            var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                            if (result.code == 200) {
                                window.top.xhTop.showSuccess(result.msg);
                                location.reload();
                            } else {
                                window.top.xhTop.showError('错误',result.error);
                            }
                        }
                    });
                },
                deleteImgAction:function (dom) {
                    var id  = dom.attr('nodeId');
                    var url = dom.attr('nodeurl');
                    xhExtra.askChangeStatus('hrm/staff_info/delAccessory',{id:id,url:url},null,'确认要删除么?',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        location.reload();
                    })
                }
                // emptyImgAction:function (dom) {
                //     xhExtra.askChangeStatus('hrm/staff_info/delAccessory',{id:id,url:url},null,'确认要删除么?',function (res) {
                //         window.top.xhTop.showSuccess(res.msg);
                //         location.reload();
                //     })
                // }
            }
        }
    }
};